dofile("StylusBox2.lua")
dofile("libColor.lua")

table.stringMax = function (t)
	local sizeMax, index = 0, 0
	for i = 1, #t do
		if type(t[i]) == "string" then
			if #t[i] > sizeMax then
				sizeMax, index =  #t[i], i
			end
		end
	end
	return sizeMax, index
end

ContextMenu = {
	new = function (x, y, choices)
		local isFile = false
		local isFolder = false
		local fileID = -1
		local fileLV = -1
		local nbFolders = {0}
		local folders = {}
		local visible = {}
		
		local setVisible = function(self, xx, yy)
			visible[1][1] = true
			if xx ~= nil then
				x = xx
			end
			if yy ~= nil then
				y = yy
			end
		end
		
		local getVisible = function(self, lv, id)
			if lv == nil and id == nil then
				return visible[1][1]
			end
			return visible[lv][id]
		end
		
		skimTable = function(t, lv, x, y)
			local maxSize, maxSizeIndex = table.stringMax(t)
			
			if nbFolders[lv] == nil then
				nbFolders[lv] = 0
			end
			
			if folders[lv] == nil then
				folders[lv] = {0}
			end
			
			if visible[lv] == nil then
				visible[lv] = {}
			end
			
			for i = 1, #t do
				if type(t[i]) == "string" then
					local it = i
					
					for j = 1, nbFolders[lv] do
						if i >= folders[lv][j] then
							i = i-nbFolders[lv]
							it = i+nbFolders[lv]
						end
					end
					
					if visible[lv][1] then
						local x1, y1, x2, y2 = x, y+((i-1)*10), x+((maxSize*6)+4), y+(i*10)+1
						screen.drawFillRect(SCREEN_DOWN, x1, y1, x2, y2, c_grey)
						screen.drawRect(SCREEN_DOWN, x1, y1, x2, y2, c_white)
						screen.print(SCREEN_DOWN, x1+2, y1+2, t[it], c_black)
						
						if StylusBox.simplePressed(x1, y1, x2, y2, Stylus.X, Stylus.Y) then
							if i == folders[lv][1]-1 then
								isFile = false
								isFolder = true
								visible[lv+1][1] = not visible[lv+1][1]
							else
								isFolder = false
								isFile = true
							end
							
							fileID, fileLV = i, lv
						end
					end
					
				elseif type(t[i]) == "table" then
					if string.sub(t[i-1], -1) ~= ">" then
						t[i-1] = t[i-1]..string.rep(" ", (maxSize-#t[i-1])-1)..">"
					end
					
					if t[i].a == nil then
						nbFolders[lv] = nbFolders[lv]+1
						
						folders[lv][nbFolders[lv]] = i
						visible[lv][nbFolders[lv]] = false
						
						t[i].a = true
					end
					skimTable(t[i], lv+1, (x+((maxSize*6)+4))-1, y+((i-2)*10))
				end
			end
				--		Mls.gui:writeToConsole(nbFolders[lv].." / "..lv)
		end
		
		local draw = function (self)
			skimTable(choices, 1, x, y)
		end
		
		local isClicked = function(self, id, lv)
			if fileID == id and fileLV == lv then
				return true
			end
			return false
		end
		
		local getClicked = function(self)
			local type = ""
			
			if isFile then
				type = "file"
			elseif isFolder then
				type = "folder"
			end
			
			return fileID, fileLV, type
		end
		
		local clear = function (self)
			self = nil
		end
		
		return {
			draw = draw,
			isClicked = isClicked,
			getClicked = getClicked,
			setVisible = setVisible,
			getVisible = getVisible,
			clear = clear
		}
	end
}
